var express = require('express');
var router = express.Router();
var UserModel = require("../model/User");
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('login', { title: '' });
});

router.get('/index', function(req, res, next) {
	console.log(req.session);
	if(req.session == null || req.session.username == null) {
		res.redirect("/");
		return;
	}
	res.render('index', {});
	
});

router.get('/prolist', function(req, res, next) {
  	res.render('prolist', { title: '' });	
});

router.get('/update', function(req, res, next) {
  	var goods_id = req.query.goods_id;
  	var result = {
  		code: 1,
		goods_id: goods_id,
		message: "删除成功"
	};
  	UserModel.ProModel.update({serial:goods_id},{$set:{flag:0}},function(err){
  		if(err){
  			console.log(err);
  		}else{
  			res.json(result);
  		}
  	});	
});

router.get('/list',function(req,res,next) {
	var condition = req.query.condition;
	var pageNO = req.query.pageNO || 1;
	pageNO = parseInt(pageNO);
	var perPageCnt = req.query.perPageCnt || 15;
	perPageCnt = parseInt(perPageCnt);

	UserModel.ProModel.count({goods_name:{$regex:condition},flag:1},function(err,count){
		var query = UserModel.ProModel.find({goods_name:{$regex:condition},flag:1}).skip((pageNO-1)*perPageCnt).limit(perPageCnt);
		query.exec(function(err,docs){
			var result = {
				total: count,
				data: docs,
				pageNO: pageNO
			}
			res.json(result);
		})
	})
})


router.get('/add', function(req, res, next) {
  res.render('add', { title: '' });
});
router.post('/addpro', function(req, res, next) {
	
	UserModel.ProModel.count({}, function(err, count){	
		var serial = 100;
		serial += count;
		var goods_name = req.body.goods_name;
	    var goods_name_color = req.body.goods_name_color;
	    var goods_name_style = req.body.goods_name_style;
	    var goods_sn = req.body.goods_sn || new Date().getTime();
	    var is_best = req.body.is_best;
	    var is_new = req.body.is_new;
	    var is_hot = req.body.is_hot;
	    var is_on_sale = req.body.is_on_sale;
	    var shop_price = req.body.shop_price;
	    var virtual_sales = req.body.virtual_sales;
	    var goods_number = req.body.goods_number;
	    var flag = 1;
	    var result = {
			code: 1,
			message: "加入成功"
		};


		var pm = new UserModel.ProModel();
		pm.serial = serial;
		pm.goods_name = goods_name;
		pm.goods_name_color = goods_name_color;
		pm.goods_name_style = goods_name_style;
		pm.goods_sn = goods_sn;
		pm.is_best = is_best;
	    pm.is_new = is_new;
	    pm.is_hot = is_hot;
	    pm.is_on_sale = is_on_sale;
		pm.shop_price = shop_price;
		pm.virtual_sales = virtual_sales;
		pm.goods_number = goods_number;
		pm.flag = flag;
		pm.save(function(err){
			if(err) {
					result.code = -110;
					result.message = "存储失败";
					res.send("存储失败");
			}
			res.json(result);
		})
	})
  	
});

router.post('/loginAction', function(req, res, next) {
	var username = req.body.username;
	var pwd = req.body.pwd;
	var result = {
		code: 1,
		message: "登录成功"
	};
	UserModel.UserModel.find({username: username, pwd: pwd}, (err, docs)=>{
		if(docs.length == 0) {
			result.code = -101;
			result.message = "您的账号或密码错误。请重新登录。"
		} else {
			req.session.username = username;
			console.log(req.session);
		}
		res.json(result);
	})
})

module.exports = router;
